home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / switchf.arc / switchf.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  5KB  |  276 lines

  1.  
  2.         /***************************************\
  3.         *                    *
  4.         *    switchf.c            *
  5.         *     Gdos-less Gdos font switcher    *
  6.         *     soon to appear in Gulam, Xmdm  *
  7.         *     and Zmdm            *
  8.         *                    *
  9.         *        Jwahar R. Bammi        *
  10.         *  {decvax,cbosgd,sun}!cwruecmp!bammi   *
  11.         *  CSnet,Arpa: bammi@cwru.edu        *
  12.         *  CIS:        71515,155        *
  13.         *                    *
  14.         \***************************************/
  15.  
  16.  
  17. #include <stdio.h>
  18. #include "aline.h"
  19.  
  20. #define SCREENFONT 2    /* index of 8x16 monochrome system default font    */
  21.  
  22.     /* Possible Errors */
  23. #define ENO_ERROR    0    /* No error                */
  24. #define EOPEN_FAIL    1    /* Error opening font file        */
  25. #define EHEAD_READ    2    /* Error reading font header        */
  26. #define EFORM_READ    3    /* Error reading font form        */
  27. #define EOFF_READ    4    /* Error reading offset table        */
  28. #define EMEMORY        5    /* Outa memory                */
  29.  
  30.     /* Globals */
  31. FONT *system_font;    /* pointer to default system font */
  32. int  ferr;        /* error # if any          */
  33.  
  34.  
  35. /*
  36.  * Init aline & get system font pointer
  37.  */
  38. init()
  39. {
  40.     init_aline();
  41.     system_font = fonts[SCREENFONT];    /* save it */
  42. }
  43.  
  44. /*
  45.  * Switch to given Gdos font (pointer)
  46.  * Acknowledgements to 
  47.  *    Martin Minow
  48.  *        minow%thundr.dec@decwrl.dec.com
  49.  *        decvax!minow
  50.  * for discovering the info in the routine below. thanks!
  51.  */
  52. switch_font(fp)
  53. FONT *fp;
  54. {
  55.     /* See aline.h for description of fields */
  56.  
  57.     naline->V_CEL_HT = fp->form_height;
  58.     naline->V_CEL_WR = aline->VWRAP * fp->form_height;
  59.     naline->V_CEL_MY = (naline->V_Y_MAX / fp->form_height) - 1;
  60.     naline->V_CEL_MX = (naline->V_X_MAX / fp->max_cell_width) - 1;
  61.     naline->V_FNT_WR = fp->form_width;
  62.     naline->V_FNT_ST = fp->first_ade;
  63.     naline->V_FNT_ND = fp->last_ade;
  64.     naline->V_OFF_AD = fp->off_table;
  65.     naline->V_FNT_AD =  fp->dat_table;
  66. }
  67.  
  68. /*
  69.  * Swap bytes of a word
  70.  */
  71. static VOID Swap(p)
  72. unsigned char p[];
  73. {
  74.     register unsigned char t;
  75.  
  76.     t = p[0];
  77.     p[0] = p[1];
  78.     p[1] = t;
  79.  
  80. }
  81.  
  82.  
  83. /*
  84.  * Swap all entries in Intel format to MC68K format WORDs
  85.  *
  86.  */
  87. static VOID fix_font(font)
  88. register FONT *font;
  89. {
  90.     
  91.     Swap(&font->font_id);
  92.     Swap(&font->size);
  93.     Swap(&font->first_ade);
  94.     Swap(&font->last_ade);
  95.     Swap(&font->top);
  96.     Swap(&font->ascent);
  97.     Swap(&font->half);
  98.     Swap(&font->descent);
  99.     Swap(&font->bottom);
  100.     Swap(&font->max_char_width);
  101.     Swap(&font->max_cell_width);
  102.     Swap(&font->left_offset);
  103.     Swap(&font->right_offset);
  104.     Swap(&font->thicken);
  105.     Swap(&font->ul_size);
  106.     Swap(&font->lighten);
  107.     Swap(&font->skew);
  108.     Swap(&font->flags);
  109.     Swap(&font->form_width);
  110.     Swap(&font->form_height);
  111.  
  112.     /* init only */
  113.     font->h_table = (char *)NULL;
  114.     font->next_font = (FONT *)NULL;
  115. }
  116.  
  117.  
  118.  
  119. /*
  120.  * Load a font, return font pointer
  121.  * On error: ferr contains error # and pointer returned is NULL
  122.  */
  123. FONT *load_font(name)
  124. char *name;
  125. {
  126.     register WORD i,j;
  127.     register FONT *font;
  128.     WORD *otable;
  129.     WORD ndata, nchar;
  130.     FILE *fp;
  131.     extern char *malloc();
  132. #ifdef ALCYON
  133.     extern FILE *fopenb();
  134. #else
  135.     extern FILE *fopen();
  136. #endif
  137.  
  138.     ferr = ENO_ERROR;
  139.  
  140.     /* open it up */
  141. #ifdef ALCYON
  142.     if((fp = fopenb(name, "r")) == (FILE *)NULL)
  143. #else
  144.     if((fp = fopen(name, "rb")) == (FILE *)NULL)
  145. #endif
  146.     {
  147.         ferr = EOPEN_FAIL;
  148.         return (FONT *)NULL;
  149.     }
  150.  
  151.     /* alloc font header */
  152.     if((font = (FONT *)malloc(sizeof(FONT))) == (FONT *)NULL)
  153.     {
  154.         ferr = EMEMORY;
  155.         return (FONT *)NULL;
  156.     }
  157.  
  158.     if(fread(font, sizeof(FONT), 1, fp) != 1)
  159.     {
  160.         ferr = EHEAD_READ;
  161.         return (FONT *)NULL;
  162.     }
  163.  
  164.     /* Fix WORD fields and init */
  165.     fix_font(font);
  166.  
  167.     /* alloc offset table */        
  168.     nchar = font->last_ade - font->first_ade + 1;    /* # of chars in font */
  169.     /* all we are doing here is if first_ade is > 30, we are makeing
  170.      * first_ade = 0, by adding 30 entries before first_ade and
  171.      * moving first_ade to 0, otherwise Bconout([2,5],X) will not
  172.      * work (they bomb) - Yet Another Undocumented 'Feature' :-)
  173.      */
  174.  
  175.     /* add one for the last offset */
  176.     i = (((nchar+30) < 256)? nchar+30 : nchar + (256 - nchar)) + 1;
  177.     if((font->off_table = (WORD *)malloc(i*2)) == (WORD *)NULL)
  178.     {
  179.         ferr = EMEMORY;
  180.         return (FONT *)NULL;
  181.     }
  182.     /* init it */
  183.     for(j = 0; j < i; j++)
  184.         font->off_table[j] = 0;
  185.  
  186.     otable = ((nchar+30) < 256)? &font->off_table[30] :
  187.              &font->off_table[(256-nchar)];
  188.     font->first_ade = 0;
  189.     
  190.     /* alloc font form table */
  191.     ndata = font->form_width * font->form_height;    /* bytes in font form */
  192.     if((font->dat_table = malloc(ndata)) == (char *)NULL)
  193.     {
  194.         ferr = EMEMORY;
  195.         return (FONT *)NULL;
  196.     }
  197.  
  198.     /* read in offset table */
  199.     if(fread(otable, 2, nchar, fp) != nchar)
  200.     {
  201.         ferr = EOFF_READ;
  202.         return (FONT *)NULL;
  203.     }
  204.     /* swap WORDs */
  205.     for(i = 0; i <= nchar; i++)
  206.         Swap(&otable[i]);
  207.  
  208.     /* read in font form */
  209.     if(fread(font->dat_table, 1, ndata, fp) != ndata)
  210.     {
  211.         ferr = EFORM_READ;
  212.         return (FONT *)NULL;
  213.     }
  214.     fclose(fp);
  215.  
  216.     /* and finally return the pointer to font header */
  217.     return font;
  218. }
  219.  
  220. /*
  221.  * free up a loaded font
  222.  */
  223. VOID free_font(font)
  224. FONT *font;
  225. {
  226.     free(font->dat_table);
  227.     free(font->off_table);
  228.     free(font);
  229. }
  230.  
  231. #ifdef TEST
  232. /*
  233.  * load and show fonts
  234.  *
  235.  */
  236. main(argc, argv)
  237. WORD argc;
  238. char **argv;
  239. {
  240.     register WORD i, j;
  241.     register FONT *font_ptr;
  242.     extern FONT *load_font();
  243.  
  244.     if(argc < 2)
  245.     {
  246.         fprintf(stderr,"Usage: switchf fontfiles ....\n");
  247.         exit(1);
  248.     }
  249.  
  250.     init();
  251.     while(--argc > 0)
  252.     {
  253.         if((font_ptr = load_font(*++argv)) == (FONT *)NULL)
  254.             fprintf(stderr,"Trouble Loading %s\n", *argv);
  255.         else
  256.         {
  257.             fprintf(stderr,"\n%s:\n", *argv);
  258.             switch_font(font_ptr);
  259.             for(j = 0, i = ' '; i < font_ptr->last_ade; i++)
  260.             {
  261.                 putchar(i);
  262.                 if((++j%8) == 0)
  263.                     putchar('\n');
  264.                 else
  265.                     putchar('\t');
  266.             }
  267.             putchar('\n');
  268.             free(font_ptr);
  269.             switch_font(system_font);
  270.         }
  271.     }
  272.     exit(0);
  273. }
  274.  
  275. #endif
  276.